package oldtest;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

import boardgame.GameState;
import boardgame.Move;
import boardgame.Piece;

public class TTT implements GameState {

	public int player = 1;
	public Vector<Chip> pieces = new Vector<Chip>();
	public boolean end = false;
	
	@Override
	public GameState applyMove(Move move) {
		pieces.add((Chip)move);
		player = player%2+1;
		end = checkGame();
		return this;
	}

	public boolean checkGame(){
		boolean[][][] chips = inPlay();
		for (int i = 0; i < 2; i++){
			for (int j = 0; j < 3; j++){
				if (chips[i][j][0]&&chips[i][j][1]&&chips[i][j][2])
					return true;
				if (chips[i][0][j]&&chips[i][1][j]&&chips[i][2][j])
					return true;
			}
			if (chips[i][0][0]&&chips[i][1][1]&&chips[i][2][2])
				return true;
			if (chips[i][0][2]&&chips[i][1][1]&&chips[i][2][0])
				return true;
		}		
		return false;
	}
	
	public boolean[][][] inPlay(){
		Iterator<Chip> i = pieces.iterator();
		boolean[][][] occupied = new boolean[2][3][3];		
		Arrays.fill(occupied[0][0], false);		
		Arrays.fill(occupied[0][1], false);
		Arrays.fill(occupied[0][2], false);
		Arrays.fill(occupied[1][0], false);		
		Arrays.fill(occupied[1][1], false);
		Arrays.fill(occupied[1][2], false);
		while (i.hasNext()){
			Chip cp = i.next();
			int[] pos = cp.position;
			int plyr = cp.owner;
			occupied[plyr-1][pos[0]][pos[1]] = true;
		}
		return occupied;
	}
	
	@Override
	public int evaluate() {
		boolean[][][] occupied = inPlay();
		int value = 0;
		int o = player%2;
		int c = player-1;		
		for (int i = 0; i < 3; i++){			
			if(occupied[c][0][i]||occupied[c][1][i]||occupied[c][2][i]) {
				if (!occupied[o][0][i]&&!occupied[o][1][i]&&!occupied[o][2][i])
					value += 1;		
				if (occupied[c][0][i]&&occupied[c][1][i]&&occupied[c][2][i])
					return 9;
			} else {
				if (occupied[o][0][i]||occupied[o][1][i]||occupied[o][2][i])
					value -= 1;
				if (occupied[o][0][i]&&occupied[o][1][i]&&occupied[o][2][i])
					return -9;
			}			
			if (occupied[c][i][0]||occupied[c][i][1]||occupied[c][i][2]){
				if (!occupied[o][i][0]&&!occupied[o][i][1]&&!occupied[o][i][2])
					value += 1;				
				if (occupied[c][i][0]&&occupied[c][i][1]&&occupied[c][i][2])
					return 9;
			} else {
				if(occupied[o][i][0]||occupied[o][i][1]||occupied[o][i][2])
					value -= 1;
				if(occupied[o][i][0]&&occupied[o][i][1]&&occupied[o][i][2])
					return -9;
			}
		}
		if (occupied[c][0][0]||occupied[c][1][1]||occupied[c][2][2]){
			if (!occupied[o][0][0]&&!occupied[c][1][1]&&!occupied[c][2][2])
				value += 1;
			if (occupied[c][0][0]||occupied[c][1][1]||occupied[c][2][2])
				return 9;
		} else {
			if (occupied[o][0][0]||occupied[c][1][1]||occupied[c][2][2])
				value -= 1;
			if (occupied[o][0][0]&&occupied[c][1][1]&&occupied[c][2][2])
				return -9;
		}			
		if (occupied[c][0][2]||occupied[c][1][1]||occupied[c][2][0]){
			if (!occupied[o][0][2]&&!occupied[c][1][1]&&!occupied[c][2][0])
				value += 1;
			if (occupied[c][0][2]||occupied[c][1][1]||occupied[c][2][0])
				return 9;
		} else {
			if (occupied[o][0][2]||occupied[c][1][1]||occupied[c][2][0])
				value -= 1;
			if (occupied[o][0][2]&&occupied[c][1][1]&&occupied[c][2][0])
				return 9;
		}					
		return value;
	}

	@Override
	public Vector<Move> generateMoves() {
		Vector<Move> move = new Vector<Move>();
		if (end)
			return move;
		boolean[][][] chips = inPlay();
		for (int i = 0; i < 3; i++){
			for (int j = 0; j < 3; j++){
				if (!chips[0][i][j]&&!chips[1][i][j]){
					int pos[] = {i,j};
					move.add((Move)(new Chip(pos, player)));
				}				
			}
		}
		return move;
	}

	@Override
	public GameState simulateMove(Move move) {		
		return applyMove(move);
	}

}
